/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/** Class which represents a user profile */
public final class Profile {
	/** Profile name */
	private String profileName;

	private String profilePassword;
	private String unhashedProfilePassword;

	/** User email */
	private String email;

	private String replyTo;

	/** Incoming server's type (protocol) */
	private String type;

	/** Incoming server */
	private String incoming;

	/** Incoming server's port number */
	private int incomingPort;

	private boolean useSSL;

	/** Outgoing server */
	private String outgoing;

	/** Outgoing server's port number */
	private int outgoingPort;

	/** User login */
	private String login;

	/** User password */
	private String password;

	private String decryptedMailPassword;

	private String signature;

	/** Tells wether this profile uses a LDAP server or not */
	private boolean ldapUser;

	/** URL of the LDAP directory */
	private String url;

	/** LDAP server's port number */
	private int port;

	/** Sufix to be used when querying the LDAP directory */
	private String suffix;

	/** Tells wether the suffix must be added to the URL */
	private boolean addSuffixToURL;

	/** Tells wether the suffix must be added to the base */
	private boolean addSuffixToBase;

	/** Tells wether the suffix must be added to the login */
	private boolean addSuffixToLogin;

	/** Base to be used when querying the LDAP directory */
	private String base;

	/** LDAP server's login */
	private String ldapLogin;

	/** LDAP server'password */
	private String ldapPassword;

	/** Name of the "name" field */
	private String ldapNameField;

	/** Name of the "mail" field */
	private String ldapMailField;

	/** Number of extra fields */
	private int nbSupFields;

	/** Extra fields defined by the user */
	private String[] supFields;

	/** Filters defined by user */
	private Vector filters;

	/** Addresses stored in the profile */
	private Vector addresses;

	private Vector ldapAddresses;

	public Profile() {

	}

	/**
	 * Constructor
	 *
	 * @param profileName
	 *            profile name
	 * @param profilePassword
	 *            profile password (hashed)
	 * @param email
	 *            user email
	 * @param type
	 *            incoming server type
	 * @param incoming
	 *            incoming server
	 * @param incomingPort
	 *            incoming server's port number
	 * @param useSSl
	 *            tells if the profiles uses a SSL connection
	 * @param outgoing
	 *            outgoing server
	 * @param outgoingPort
	 *            outgoing server's port number
	 * @param login
	 *            user login
	 * @param password
	 *            user password
	 * @param signature
	 *            signature file
	 * @param ldapUser
	 *            tells if the profile uses a LDAP server or not
	 * @param filters
	 *            filters defined by user
	 * @param addresses
	 *            addresses stored in the profile
	 */
	public Profile(String profileName, String profilePassword, String email, String replyTo, String type, String incoming, int incomingPort, boolean useSSL, String outgoing, int outgoingPort,
			String login, String password, String signature, boolean ldapUser, Vector filters, Vector addresses) {
		this.profileName = profileName;
		this.profilePassword = profilePassword;
		this.email = email;
		this.replyTo = replyTo;
		this.type = type;
		this.incoming = incoming;
		this.incomingPort = incomingPort;
		this.useSSL = useSSL;
		this.outgoing = outgoing;
		this.outgoingPort = outgoingPort;
		this.login = login;
		this.password = password;
		this.signature = signature;
		this.ldapUser = ldapUser;
		this.filters = filters;
		this.addresses = addresses;

		ldapAddresses = new Vector();
	}

	/**
	 * Constructor
	 *
	 * @param profileName
	 *            profile name
	 * @param profilePassword
	 *            profile password (hashed)
	 * @param email
	 *            user email
	 * @param type
	 *            incoming server type
	 * @param incoming
	 *            incoming server
	 * @param incomingPort
	 *            incoming server's port number
	 * @param useSSl
	 *            tells if the profiles uses a SSL connection
	 * @param outgoing
	 *            outgoing server
	 * @param outgoingPort
	 *            outgoing server's port number
	 * @param login
	 *            user login
	 * @param password
	 *            user password
	 * @param signature
	 *            signature file
	 * @param ldapUser
	 *            tells if the profile uses a LDAP server or not
	 * @param url
	 *            LDAP server's url
	 * @param port
	 *            LDAP server's port number
	 * @param suffix
	 *            suffix to be used when querying the LDAP directory
	 * @param addSuffixToURL
	 *            tells wether the suffix must be added to the URL
	 * @param addSuffixToBase
	 *            tells wether the suffix must be added to the base
	 * @param addSuffixToLogin
	 *            tells wether the suffix must be added to the login
	 * @param base
	 *            base to be used when querying the LDAP directory
	 * @param ldapLogin
	 *            LDAP server's login
	 * @param ldapPassword
	 *            LDAP server'password
	 * @param ldapNameField
	 *            name of the "name" field
	 * @param ldapMailField
	 *            name of the "mail" field
	 * @param nbSupFields
	 *            number of extra fields
	 * @param supFields
	 *            extra fields defined by the user
	 * @param filters
	 *            filters defined by user
	 * @param addresses
	 *            addresses stored in the profile
	 */
	public Profile(String profileName, String profilePassword, String email, String replyTo, String type, String incoming, int incomingPort, boolean useSSL, String outgoing, int outgoingPort,
			String login, String password, String signature, boolean ldapUser, String url, int port, String suffix, boolean addSuffixToURL, boolean addSuffixToBase, boolean addSuffixToLogin,
			String base, String ldapLogin, String ldapPassword, String ldapNameField, String ldapMailField, int nbSupFields, String[] supFields, Vector filters, Vector addresses) {
		this(profileName, profilePassword, email, replyTo, type, incoming, incomingPort, useSSL, outgoing, outgoingPort, login, password, signature, ldapUser, filters, addresses);

		this.url = url;
		this.port = port;
		this.suffix = suffix;
		this.addSuffixToURL = addSuffixToURL;
		this.addSuffixToBase = addSuffixToBase;
		this.addSuffixToLogin = addSuffixToLogin;
		this.base = base;
		this.ldapLogin = ldapLogin;
		this.ldapPassword = ldapPassword;
		this.ldapNameField = ldapNameField;
		this.ldapMailField = ldapMailField;
		this.nbSupFields = nbSupFields;
		this.supFields = supFields;
	}

	/**
	 * Method to load a profile from a file
	 *
	 * @param profileName
	 *            profile name
	 * @param passwordEntered
	 *            clear-text password
	 * @return the profile object
	 */
	/**
	 * Method to load a profile from a file
	 *
	 * @param profileName
	 *            profile name
	 * @param passwordEntered
	 *            clear-text password
	 * @return the profile object
	 */
	protected final static Profile loadProfile(String profileName, String passwordEntered, ResourceBundle msgBundle) {
		Profile p = null;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(new File(profileName + ".prf"));

			Element profileElement = document.getDocumentElement();

			String pass1 = JMailSecurity.hashAndEncode(passwordEntered);
			String profilePassword = profileElement.getAttribute("password");

			if (pass1.compareTo(profilePassword) != 0) {
				JOptionPane.showMessageDialog(null, msgBundle.getString("Profile.incorrectPasswordText"), "Profile", JOptionPane.ERROR_MESSAGE);
				return (null);
			}

			NodeList accountsList = profileElement.getElementsByTagName("Account");
			Element account = (Element) accountsList.item(0);

			String email = account.getAttribute("email");
			String replyTo = account.getAttribute("replyTo");

			NodeList miscList = account.getElementsByTagName("Misc");
			Element misc = (Element) miscList.item(0);

			String login = misc.getAttribute("login");
			String password = misc.getAttribute("password");
			String signature = misc.getAttribute("signature");

			NodeList serversList = account.getElementsByTagName("Servers");
			Element servers = (Element) serversList.item(0);

			String incoming = servers.getAttribute("incoming");
			int incomingPort = Integer.parseInt(servers.getAttribute("incomingPort"));
			boolean useSSL = Boolean.valueOf(servers.getAttribute("useSSL")).booleanValue();
			String type = servers.getAttribute("type");
			String outgoing = servers.getAttribute("outgoing");
			int outgoingPort = Integer.parseInt(servers.getAttribute("outgoingPort"));

			Vector filters = new Vector();

			NodeList filtersList = account.getElementsByTagName("Filters");
			Element filtersElement = (Element) filtersList.item(0);

			if (filtersElement != null) {
				NodeList filterList = filtersElement.getElementsByTagName("Filter");

				Element filter;
				int length = filterList.getLength();

				for (int i = 0; i < length; i++) {
					filter = (Element) filterList.item(i);

					filters.add(new Filter(filter.getAttribute("name"), filter.getAttribute("condition"), filter.getAttribute("trigger"), filter.getAttribute("action"), filter.getAttribute("target"),
							Boolean.valueOf(filter.getAttribute("activated")).booleanValue()));
				}
			}

			NodeList ldapList = account.getElementsByTagName("LDAP");

			boolean ldapUser = false;

			if (ldapList.getLength() > 0)
				ldapUser = true;

			Vector addresses = new Vector();

			if (!ldapUser) {
				NodeList entriesList = profileElement.getElementsByTagName("Addresses");
				Element addressesElement = (Element) entriesList.item(0);

				if (addressesElement != null) {
					NodeList entries = addressesElement.getElementsByTagName("Entry");

					int length = entries.getLength();
					Element entry;

					for (int i = 0; i < length; i++) {
						entry = (Element) entries.item(i);

						addresses.add(new Entry(entry.getAttribute("name"), entry.getAttribute("email")));
					}
				}

				p = new Profile(profileName, profilePassword, email, replyTo, type, incoming, incomingPort, useSSL, outgoing, outgoingPort, login, password, signature, ldapUser, filters, addresses);
			}

			else {
				Element ldap = (Element) ldapList.item(0);

				String url = ldap.getAttribute("url");
				int port = Integer.parseInt(ldap.getAttribute("port"));

				NodeList suffixList = ldap.getElementsByTagName("Suffix");
				Element suffixElement = (Element) suffixList.item(0);

				String suffix = suffixElement.getAttribute("name");
				boolean addSuffixToURL = getBooleanElementValue(suffixElement, "addToURL");
				boolean addSuffixToBase = getBooleanElementValue(suffixElement, "addToBase");
				boolean addSuffixToLogin = getBooleanElementValue(suffixElement, "addToLogin");
				NodeList miscList2 = ldap.getElementsByTagName("Misc");
				Element misc2 = (Element) miscList2.item(0);

				String base = misc2.getAttribute("base");
				String ldapLogin = misc2.getAttribute("login");
				String ldapPassword = misc2.getAttribute("password");

				NodeList fieldsList = ldap.getElementsByTagName("Fields");
				Element fields = (Element) fieldsList.item(0);

				String ldapNameField = fields.getAttribute("name");
				String ldapMailField = fields.getAttribute("mail");

				int nbSupFields = 0;
				String[] supFields = null;

				NodeList suppFieldsList = ldap.getElementsByTagName("SuppFields");
				Element suppFieldsElement = (Element) suppFieldsList.item(0);

				if (suppFieldsElement != null) {
					NodeList suppFieldList = suppFieldsElement.getElementsByTagName("SuppField");
					nbSupFields = suppFieldList.getLength();

					supFields = new String[nbSupFields];

					for (int i = 0; i < nbSupFields; i++) {
						Element suppField = (Element) suppFieldList.item(i);
						supFields[i] = suppField.getAttribute("name");
					}
				}

				NodeList entriesList = profileElement.getElementsByTagName("Addresses");
				Element addressesElement = (Element) entriesList.item(0);

				if (addressesElement != null) {
					NodeList entries = addressesElement.getElementsByTagName("Entry");

					int length = entries.getLength();
					Element entry;

					for (int i = 0; i < length; i++) {
						entry = (Element) entries.item(i);
						Entry e = new Entry(entry.getAttribute("name"), entry.getAttribute("email"));
						addresses.add(e);
					}
				}

				p = new Profile(profileName, profilePassword, email, replyTo, type, incoming, incomingPort, useSSL, outgoing, outgoingPort, login, password, signature, ldapUser, url, port, suffix,
						addSuffixToURL, addSuffixToBase, addSuffixToLogin, base, ldapLogin, ldapPassword, ldapNameField, ldapMailField, nbSupFields, supFields, filters, addresses);
			}
		}

		catch (SAXParseException saxE) {
			JOptionPane.showMessageDialog(null, saxE.getMessage(), "SAXParseException", JOptionPane.ERROR_MESSAGE);
			p = null;
		}

		catch (NumberFormatException ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage(), "NumberFormatException", JOptionPane.ERROR_MESSAGE);
			p = null;
		}

		catch (IOException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "IOException", JOptionPane.ERROR_MESSAGE);
			p = null;
		}

		catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE);
			p = null;
		}

		p.setUnhashedProfilePassword(passwordEntered);

		/*
		 * If everything was OK, decrypt the mail password using the clear
		 * profile password
		 */
		p.setDecryptedMailPassword(JMailSecurity.decodeAndDecrypt(passwordEntered, p.getPassword()));

		return (p);
	}

	public static boolean getBooleanElementValue(Element suffixElement, String attributeName) {
		if (suffixElement == null || attributeName == null) {
			return false;
		}
		String attribute = suffixElement.getAttribute(attributeName);
		if (attribute == null) {
			return false;
		}
		Boolean value = Boolean.valueOf(attribute);
		if (value == null) {
			return false;
		}
		return value.booleanValue();
	}

	/**
	 * Returns the profile name
	 *
	 * @return profile name
	 */
	public String getProfileName() {
		return profileName;
	}

	public String getProfilePassword() {
		return profilePassword;
	}

	public void setProfilePassword(String profilePassword) {
		this.profilePassword = profilePassword;
	}

	public String getUnhashedProfilePassword() {
		return unhashedProfilePassword;
	}

	public void setUnhashedProfilePassword(String unhashedProfilePassword) {
		this.unhashedProfilePassword = unhashedProfilePassword;
	}

	/**
	 * Returns the email address
	 *
	 * @return the email address
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Sets the new email address
	 *
	 * @param email
	 *            the new email address
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	public String getReplyTo() {
		return replyTo;
	}

	public void setReplyTo(String replyTo) {
		this.replyTo = replyTo;
	}

	/**
	 * Returns the server's type
	 *
	 * @return server type
	 */
	public String getType() {
		return type;
	}

	/**
	 * Sets the new server type
	 *
	 * @param type
	 *            the new type
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * Returns the incoming server
	 *
	 * @return incoming server
	 */
	public String getIncoming() {
		return incoming;
	}

	/**
	 * Sets the new incoming server
	 *
	 * @param incoming
	 *            new incoming server
	 */
	public void setIncoming(String incoming) {
		this.incoming = incoming;
	}

	/**
	 * Returns the incoming server's port number
	 *
	 * @return incoming server's port number
	 */
	public int getIncomingPort() {
		return incomingPort;
	}

	/**
	 * Sets the new incoming server's port number
	 *
	 * @param incomingPort
	 *            the new incoming server's port number
	 */
	public void setIncomingPort(int incomingPort) {
		this.incomingPort = incomingPort;
	}

	public boolean getUseSSL() {
		return useSSL;
	}

	public void setUseSSL(boolean useSSL) {
		this.useSSL = useSSL;
	}

	/**
	 * Returns the outgoing server
	 *
	 * @return outgoing server
	 */
	public String getOutgoing() {
		return outgoing;
	}

	/**
	 * Sets the new outgoing server
	 *
	 * @param outgoing
	 *            the new outgoing server
	 */
	public void setOutgoing(String outgoing) {
		this.outgoing = outgoing;
	}

	/**
	 * Returns the outgoing server's port number
	 *
	 * @return outgoing server's port number
	 */
	public int getOutgoingPort() {
		return outgoingPort;
	}

	/**
	 * Sets the new outgoing server's port number
	 *
	 * @param outgoingPort
	 *            the new outgoing server's port number
	 */
	public void setOutgoingPort(int outgoingPort) {
		this.outgoingPort = outgoingPort;
	}

	/**
	 * Returns the login
	 *
	 * @return login
	 */
	public String getLogin() {
		return login;
	}

	/**
	 * Sets the new login
	 *
	 * @param login
	 *            new login
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * Returns the password
	 *
	 * @return password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Sets the new password
	 *
	 * @param password
	 *            new password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	public String getDecryptedMailPassword() {
		return decryptedMailPassword;
	}

	public void setDecryptedMailPassword(String decryptedMailPassword) {
		this.decryptedMailPassword = decryptedMailPassword;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	/**
	 * Tells wether this profile uses LDAP
	 *
	 * @return boolean
	 */
	public boolean getLdapUser() {
		return ldapUser;
	}

	/**
	 * Sets the <code>ldapUser</code> boolean
	 *
	 * @param ldapUser
	 *            new value of the boolean
	 */
	public void setLdapUser(boolean ldapUser) {
		this.ldapUser = ldapUser;
	}

	/**
	 * Sets the base infos for the profile
	 *
	 * @param email
	 *            user email
	 * @param type
	 *            incoming server type
	 * @param incoming
	 *            incoming server
	 * @param incomingPort
	 *            incoming server's port number
	 * @param outgoing
	 *            outgoing server
	 * @param outgoingPort
	 *            outgoing server's port number
	 * @param login
	 *            user login
	 * @param password
	 *            user password
	 * @param ldapUser
	 *            tells if the profile uses a LDAP server or not
	 */
	public void setBaseInfos(String profilePassword, String email, String replyTo, String type, String incoming, int incomingPort, boolean useSSL, String outgoing, int outgoingPort, String login,
			String password, String signature, boolean ldapUser) {
		this.profilePassword = profilePassword;
		this.email = email;
		this.replyTo = replyTo;
		this.type = type;
		this.incoming = incoming;
		this.incomingPort = incomingPort;
		this.useSSL = useSSL;
		this.outgoing = outgoing;
		this.outgoingPort = outgoingPort;
		this.login = login;
		this.password = password;
		this.signature = signature;
		this.ldapUser = ldapUser;
	}

	/**
	 * Returns the LDAP server's url
	 *
	 * @return LDAP server's url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * Sets the new LDAP server's url
	 *
	 * @param url
	 *            the new LDAP server's url
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * Returns the LDAP server's port number
	 *
	 * @return LDAP server's port number
	 */
	public int getPort() {
		return port;
	}

	/**
	 * Sets the new LDAP server's port number
	 *
	 * @param port
	 *            the new LDAP server's port number
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * Returns the suffix used in LDAP queries
	 *
	 * @return suffix used in LDAP queries
	 */
	public String getSuffix() {
		return suffix;
	}

	/**
	 * Sets the new suffix
	 *
	 * @param suffix
	 *            the new suffix used in LDAP queries
	 */
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	/**
	 * Returns the <code>addSuffixToURL</code> boolean
	 *
	 * @return <code>addSuffixToURL</code> boolean
	 */
	public boolean getAddSuffixToURL() {
		return addSuffixToURL;
	}

	/**
	 * Sets the value of <code>addSuffixToURL</code>
	 *
	 * @param addSuffixToURL
	 *            new value of <code>addSuffixToURL</code>
	 */
	public void setAddSuffixToURL(boolean addSuffixToURL) {
		this.addSuffixToURL = addSuffixToURL;
	}

	/**
	 * Returns the <code>addSuffixToBase</code> boolean
	 *
	 * @return <code>addSuffixToBase</code> boolean
	 */
	public boolean getAddSuffixToBase() {
		return addSuffixToBase;
	}

	/**
	 * Sets the value of <code>addSuffixToBase</code>
	 *
	 * @param addSuffixToBase
	 *            new value of <code>addSuffixToBase</code>
	 */
	public void setAddSuffixToBase(boolean addSuffixToBase) {
		this.addSuffixToBase = addSuffixToBase;
	}

	/**
	 * Returns the <code>addSuffixToLogin</code> boolean
	 *
	 * @return <code>addSuffixToLogin</code> boolean
	 */
	public boolean getAddSuffixToLogin() {
		return addSuffixToLogin;
	}

	/**
	 * Sets the value of <code>addSuffixToLogin</code>
	 *
	 * @param addSuffixToLogin
	 *            new value of <code>addSuffixToLogin</code>
	 */
	public void setAddSuffixToLogin(boolean addSuffixToLogin) {
		this.addSuffixToLogin = addSuffixToLogin;
	}

	/**
	 * Returns the base used in LDAP queries
	 *
	 * @return base used in LDAP queries
	 */
	public String getBase() {
		return base;
	}

	/**
	 * Sets the base used in LDAP queries
	 *
	 * @param base
	 *            new base
	 */
	public void setBase(String base) {
		this.base = base;
	}

	/**
	 * Returns the LDAP server login
	 *
	 * @return the LDAP login
	 */
	public String getLdapLogin() {
		return ldapLogin;
	}

	/**
	 * Sets the LDAP login
	 *
	 * @param ldapLogin
	 *            new LDAP login
	 */
	public void setLdapLogin(String ldapLogin) {
		this.ldapLogin = ldapLogin;
	}

	/**
	 * Returns the LDAP server password
	 *
	 * @return the LDAP password
	 */
	public String getLdapPassword() {
		return ldapPassword;
	}

	/**
	 * Sets the LDAP password
	 *
	 * @param ldapPassword
	 *            new LDAP password
	 */
	public void setLdapPassword(String ldapPassword) {
		this.ldapPassword = ldapPassword;
	}

	/**
	 * Returns the name of the "name" field
	 *
	 * @return name of the "name" field
	 */
	public String getLdapNameField() {
		return ldapNameField;
	}

	/**
	 * Sets the name of the "name" field
	 *
	 * @param ldapNameField
	 *            new name of the "name" field
	 */
	public void setLdapNameField(String ldapNameField) {
		this.ldapNameField = ldapNameField;
	}

	/**
	 * Returns the name of the "mail" field
	 *
	 * @return name of the "mail" field
	 */
	public String getLdapMailField() {
		return ldapMailField;
	}

	/**
	 * Sets the name of the "mail" field
	 *
	 * @param ldapMailField
	 *            new name of the "mail" field
	 */
	public void setLdapMailField(String ldapMailField) {
		this.ldapMailField = ldapMailField;
	}

	/**
	 * Returns the number of extra fields
	 *
	 * @return number of extra fields
	 */
	public int getNbSupFields() {
		return nbSupFields;
	}

	/**
	 * Sets the number of extra fields
	 *
	 * @param nbSupFields
	 *            number of extra fields
	 */
	public void setNbSupFields(int nbSupFields) {
		this.nbSupFields = nbSupFields;
	}

	/**
	 * Sets the LDAP related infos for the profile
	 *
	 * @param ldapInfo
	 *            TODO
	 */
	public void setLdapInfos(LdapInfo ldapInfo) {
		this.url = ldapInfo.getUrl();
		this.port = ldapInfo.getPort();
		this.suffix = ldapInfo.getSuffix();
		this.addSuffixToURL = ldapInfo.isAddSuffixToURL();
		this.addSuffixToBase = ldapInfo.isAddSuffixToBase();
		this.addSuffixToLogin = ldapInfo.isAddSuffixToLogin();
		this.base = ldapInfo.getBase();
		this.ldapLogin = ldapInfo.getLdapLogin();
		this.ldapPassword = ldapInfo.getLdapPassword();
		this.ldapNameField = ldapInfo.getLdapNameField();
		this.ldapMailField = ldapInfo.getLdapMailField();
		this.nbSupFields = ldapInfo.getNbSupFields();
	}

	/**
	 * Returns the extra fields
	 *
	 * @return <code>Vector</code> containing the extra fields
	 */
	public String[] getSupFields() {
		return supFields;
	}

	/**
	 * Sets the extra fields
	 *
	 * @param supFields
	 *            <code>Vector</code> containing the new extra fields
	 */
	public void setSupFields(String[] supFields) {
		this.supFields = supFields;
	}

	public void setSupFieldsVector(Vector supFields) {
		int size = supFields.size();

		this.supFields = new String[size];

		for (int i = 0; i < size; i++)
			this.supFields[i] = (String) supFields.get(i);
	}

	/**
	 * Returns the filters
	 *
	 * @return filters defined in the profile
	 */
	public Vector getFilters() {
		return filters;
	}

	/**
	 * Sets the filters
	 *
	 * @param filters
	 *            <code>Vector</code> containing the new filters
	 */
	public void setFilters(Vector filters) {
		this.filters = filters;
	}

	/**
	 * Returns the adresses stored in the profile
	 *
	 * @return <code>Vector</code> containing the adresses
	 */
	public Vector getAddresses() {
		return addresses;
	}

	public Entry[] getAddressesArray() {
		int size = addresses.size();

		Entry[] entries = new Entry[size];

		for (int i = 0; i < size; i++)
			entries[i] = (Entry) addresses.get(i);

		return (entries);
	}

	/**
	 * Sets the addresses stored in the profile
	 *
	 * @param addresses
	 *            <code>Vector</code> containing the new addresses
	 */
	public void setAddresses(Vector addresses) {
		this.addresses = addresses;
	}

	public Vector getLdapAddresses() {
		return ldapAddresses;
	}

	public Entry[] getLdapAddressesArray() {
		int size = ldapAddresses.size();

		Entry[] ldapEntries = new Entry[size];

		for (int i = 0; i < size; i++)
			ldapEntries[i] = (Entry) ldapAddresses.get(i);

		return (ldapEntries);
	}

	public void setLdapAddresses(Vector ldapAddresses) {
		this.ldapAddresses = ldapAddresses;
	}

	public Entry[] getAllAddressesArray() {
		int addressesSize = addresses.size();
		int ldapAddressesSize = ldapAddresses.size();
		int size = addressesSize + ldapAddressesSize;

		Entry[] entries = new Entry[size];
		int i, j = 0;

		for (i = 0; i < addressesSize; i++)
			entries[j++] = (Entry) addresses.get(i);

		for (i = 0; i < ldapAddressesSize; i++)
			entries[j++] = (Entry) ldapAddresses.get(i);

		return (entries);
	}

	/** Writes the profile in a file */
	protected final void save() {
		try {
			PrintWriter output = new PrintWriter(new FileWriter(profileName + ".prf"));

			output.println("<?xml version=\"1.0\"?>");
			output.println("<Profile name=\"" + profileName + "\" password=\"" + profilePassword + "\">");
			output.println("<Account email=\"" + email + "\"");

			if (replyTo != null)
				output.println(" replyTo=\"" + replyTo + "\"");

			output.println(">");

			output.println("<Misc login=\"" + login + "\" password=\"" + password + "\"");

			if (signature != null)
				output.println(" signature=\"" + signature + "\"");

			output.println("/>");
			output.println("<Servers incoming=\"" + incoming + "\" incomingPort=\"" + incomingPort + "\" useSSL=\"" + useSSL + "\" type=\"" + type + "\" outgoing=\"" + outgoing + "\" outgoingPort=\""
					+ outgoingPort + "\"/>");

			if (filters != null) {
				int size = filters.size();

				if (size > 0) {
					output.println("<Filters>");

					for (int i = 0; i < size; i++) {
						Filter f = (Filter) filters.get(i);
						f.write(output);
					}

					output.println("</Filters>");
				}
			}

			if (ldapUser) {
				output.println("<LDAP url=\"" + url + "\" port=\"" + port + "\">");
				output.println("<Suffix name=\"" + suffix + "\" addToURL=\"" + addSuffixToURL + "\" addToBase=\"" + addSuffixToBase + "\" addToLogin=\"" + addSuffixToLogin + "\"/>");
				output.println("<Misc base=\"" + base + "\" login=\"" + ldapLogin + "\" password=\"" + ldapPassword + "\"/>");
				output.println("<Fields name=\"" + ldapNameField + "\" mail=\"" + ldapMailField + "\"/>");

				if (nbSupFields > 0) {
					output.println("<SuppFields>");

					for (int i = 0; i < nbSupFields; i++)
						output.println("<SuppField name=\"" + supFields[i] + "\"/>");

					output.println("</SuppFields>");
				}

				output.println("</LDAP>");
			}

			output.println("</Account>");

			if (addresses != null) {
				int size = addresses.size();

				if (size > 0) {
					output.println("<Addresses>");

					for (int i = 0; i < size; i++) {
						Entry entry = (Entry) addresses.get(i);
						entry.writeTo(output);
					}

					output.println("</Addresses>");
				}
			}

			output.println("</Profile>");

			output.close();
		}

		catch (FileNotFoundException fileE) {
			JOptionPane.showMessageDialog(null, fileE.getMessage(), "FileNotFoundException", JOptionPane.ERROR_MESSAGE);
		}

		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setProfileName(String profileName) {
		this.profileName = profileName;
	}
}
